package cn.school.mybatis.service;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import cn.school.mybatis.dao.StudentDao;
import cn.school.mybatis.pojo.Student;
import cn.school.mybatis.util.MyBatisUtil;

//业务逻辑bean
//服务层,调用dao方法
public class StudentService {

	public final static StudentService SERVICE = new StudentService();

	public List<Student> search0(Map<String, Object> params, int pageNo, int pageSize, String orderBy) {
		pageNo = pageNo < 1 ? 1 : pageNo;

		SqlSession session = MyBatisUtil.get();
		try {
			StudentDao dao = session.getMapper(StudentDao.class);
			return dao.search0(params, pageNo, pageSize, orderBy);
		} finally {
			MyBatisUtil.close(session);
		}
	}

	public int searchTotal(Map<String, Object> params, int pageSize) {
		SqlSession session = MyBatisUtil.get();
		try {
			StudentDao dao = session.getMapper(StudentDao.class);
			int total = dao.searchTotal(params);
			// total/pageSize+(total%pageSize==0?0:1);

			return (total - 1) / pageSize + 1;
		} finally {
			MyBatisUtil.close(session);
		}
	}

	public Student get(long stuno) {
		SqlSession session = MyBatisUtil.get();
		try {
			StudentDao dao = session.getMapper(StudentDao.class);
			return dao.get(stuno);
		} finally {
			MyBatisUtil.close(session);
		}
	}

	public boolean checkTele(String phone) {
		SqlSession session = MyBatisUtil.get();
		try {
			StudentDao dao = session.getMapper(StudentDao.class);
			return dao.checkTele(phone) > 0;
		} finally {
			MyBatisUtil.close(session);
		}
	}
}
